@extends('layouts.main')

@inject('special_offer','App\Http\Presenters\ChoicePresenter')

@section('title', '开票清单')

@section('content')
<h3>@yield('title')</h3>
<div class="btn-group" role="group">
  	<a href="{{ action('BusinessData\InvoiceDetailsController@create') }}" class="btn btn-primary">添加开票信息</a>
    <a href="{{ action('BusinessData\InvoiceDetailsController@import') }}" class="btn btn-warning">导入开票明细</a>
    <a href="{{ action('BusinessData\InvoiceDetailsController@importSpecialOrder') }}" class="btn btn-warning">导入特价订单</a>
    <button id="update_category_contract" class="btn btn-danger">更新订单合同分类</button>
    <button id="update_special_order_from_material" class="btn btn-danger">根据物料更新特价订单</button>
</div>

@if(session('success'))
<div class="alert alert-success" role="alert">{{ session('success') }}</div>
@endif

<div class="clearfix"></div>
<hr>

<form class="form-inline pull-right" method="get" action="{{ action('BusinessData\InvoiceDetailsController@index') }}">
    
    <div class="form-group ">
        <label for="year">年份</label>
        <div class='input-group'>
            <input type='text' id='year' name="year" class="form-control" value="{{ $request_params['year'] }}" />
        </div>
    </div>

    <div class="form-group ">
        <label for="month">月份</label>
        <div class='input-group'>
            <input type='text' id='month' name="month" class="form-control" value="{{ $request_params['month'] }}" />
        </div>
    </div>

    <div class="form-group ">
        <label for="sales_order_no">销售订单号</label>
        <div class='input-group' id='sales_order_no'>
            <input type='text' name="sales_order_no" class="form-control" value="{{ $request_params['sales_order_no'] }}" />
        </div>
    </div>

    <button type="submit" name="search" value="search" class="btn btn-primary">查询</button>
    <button type="submit" name="delete" value="delete" class="btn btn-danger">根据条件删除数据</button>

</form>

<div class="clearfix"></div>
<hr>

<table class="table table-bordered table-striped table-hover">
    <tr>
		<th>id</th>
		<th>年份</th>
		<th>月份</th>
		<th>销售订单号</th>
		<th>行号</th>
        <th>物料</th>
        <th>开票数量</th>
        <th>开票总价(含税)(折扣前)</th>
        <th>开票总价(含税)</th>
        <th>开票总价(不含税)</th>
        <th>税额</th>
        <th>折扣金额</th>
        <th>附加费</th>
        <th>销售订单类型</th>
        <th>项目类型</th>
        <th>销售组织</th>
        <th>销售大区</th>
        <th>销售办事处</th>
        <th>分销渠道</th>
        <th>付款方</th>
        <th>业务员</th>
        <th>是否特价</th>
		<th>操作</th>
    </tr>
  @forelse($models as $model)
	<tr>
		<td>{{ $model->id }}</td>
		<td>{{ $model->year }}</td>
		<td>{{ $model->month }}</td>
		<td>{{ $model->sales_order_no }}</td>
		<td>{{ $model->sales_order_line_no }}</td>
		<td>{{ $model->material }}</td>
		<td>{{ $model->quantity }}</td>
        <td>{{ $model->total_price_tax_discount }}</td>
        <td>{{ $model->total_price_tax }}</td>
        <td>{{ $model->total_price_no_tax }}</td>
        <td>{{ $model->tax }}</td>
        <td>{{ $model->discount }}</td>
        <td>{{ $model->surcharge }}</td>
        <td>{{ $model->sales_order_type ? $model->sales_order_type->code_desc : '' }}</td>
        <td>{{ $model->project_type ? $model->project_type->code_desc : ''}}</td>
        <td>{{ $model->sales_organization->code_desc }}</td>
        <td>{{ $model->sales_region ? $model->sales_region->code_desc : ''}}</td>
        <td>{{ $model->sales_office ? $model->sales_office->code_desc : '' }}</td>
        <td>{{ $model->distribution_channel ? $model->distribution_channel->code_desc : '' }}</td>
        <td>{{ $model->payer ? $model->payer->name : '' }}</td>
        <td>{{ $model->salesman ? $model->salesman->name : '' }}</td>
        <td>{{ $special_offer->judgeWhether($model->is_special) }}</td>
		<td>
			<a href="{{ action('BusinessData\InvoiceDetailsController@edit', $model) }}" class="btn btn-warning btn-sm">编辑</a>
			<button name="delete" value="{{ $model->id }}" class="btn btn-danger btn-sm">删除</button>
		</td>
	</tr>
  @empty
  	<tr><td>暂无数据</td></tr>
  @endforelse
    <tr>
        <th>总计</th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th>{{ number_format($amount_total_price_no_tax, 2) }}</th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    </tr>
</table>
{{ $models->appends([
    'year' => $request_params['year'],
    'month' => $request_params['month'],
    'sales_order_no' => $request_params['sales_order_no'],
    ])->links() }}
<script>
      $(document).ready(function(){
        //删除单个
        $("button[name=delete]").click(function(){
            var that = $(this);
            var id = that.attr("value");
            $.post("/business_data/invoice_details/" + id, {
                '_method': 'DELETE',
                '_token': '{{ csrf_token() }}',
            }, function(data){
                switch(data.status){
                    case 0:
                        that.parent().parent('tr').remove();
                        alert("删除信息成功！");
                        break;
                }
            });
        });

        //更新订单合同分类
        $('#update_category_contract').click(function(){
            //选择范围
            var year = $('#year').val();
            var month = $('#month').val();
			if(!$.isNumeric(year) || !$.isNumeric(month)){
				alert("请选择需要更新数据的年份和月份！");
				return;
			}

            $.post("/business_data/invoice_details/update_category_contract", {
                '_token': '{{ csrf_token() }}',
                'year': year,
                'month': month,
            }, function(data){
				if(data.status == 0){
					alert("订单合同分类数据更新成功！");
				}
            });
        });


        //根据物料更新特价订单
        $('#update_special_order_from_material').click(function(){
            //选择范围
            var year = $('#year').val();
            var month = $('#month').val();
			if(!$.isNumeric(year) || !$.isNumeric(month)){
				alert("请选择需要更新数据的年份和月份！");
				return;
			}

            $.post("/business_data/invoice_details/update_special_order_from_material", {
                '_token': '{{ csrf_token() }}',
                'year': year,
                'month': month,
            }, function(data){
				if(data.status == 0){
					alert("物料特价数据更新成功！");
				}
            });
        });

    });
</script>
@endsection